package com.study_security.controller;

import com.study_security.domain.LoginUser;
import com.study_security.dto.MemberDTO;
import com.study_security.dto.ProjectDTO;
import com.study_security.entity.ProjectMember;
import com.study_security.service.MemberService;
import com.study_security.service.ProjectMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/members")
//@PreAuthorize("hasAnyAuthority('perm_review','perm_leader','perm_member')")
public class MemberController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private ProjectMemberService projectMemberService;

    /**
     * 获取所有成员
     *
     * @return 所有成员的列表
     */
    @PreAuthorize("hasAnyAuthority('perm_review','perm_leader')")
    @GetMapping
    public List<MemberDTO> getAllMembers() {
        return memberService.getAllMembers();
    }

    /**
     * 根据项目名称查找项目成员及其任务
     *
     * @param projectName 项目名称
     * @return 项目成员及其任务的列表
     */
    @GetMapping("/search")
    public List<MemberDTO> getProjectMembersAndTasks(@RequestParam String projectName) {
        return memberService.getProjectMembersAndTasks(projectName);
    }

    /**
     * 获取用户的项目及其成员信息
     *
     * @param authentication
     * @return 用户的项目
     */
    @PreAuthorize("hasAnyAuthority('perm_leader','perm_review')")
    @GetMapping("/projects")
    public List<ProjectDTO> getUserProjectsAndMembers(Authentication authentication) {
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        Long userId = loginUser.getUser().getId();
        return memberService.getUserProjects(userId);
    }

    /**
     * 根据项目ID获取项目成员
     *
     * @param projectId 项目ID
     * @return 项目成员的列表
     */
    @GetMapping("/{projectId}/members")
    public List<MemberDTO> getProjectMembers(@PathVariable Long projectId) {
        return memberService.getMembersByProjectId(projectId);
    }

    /**
     * 添加项目成员
     *
     * @param projectMember 项目成员信息
     * @return 添加结果
     */
    @PreAuthorize("hasAuthority('perm_leader')")
    @PostMapping("/add")
    public String addProjectMembers(@RequestBody ProjectMember projectMember) {
        if (projectMemberService.projectMemberExists(projectMember.getProjectId(), projectMember.getMemberId())) {
            return "The member already exists, please select again.";
        }

        boolean isAdded = projectMemberService.addMemberToProject(projectMember);
        return isAdded ? "Member added successfully!" : "Failed to add Member.";
    }

    /**
     * 移除项目成员
     *
     * @param projectId 项目ID
     * @param memberId 成员ID
     * @return 移除结果
     */
    @PreAuthorize("hasAuthority('perm_leader')")
    @DeleteMapping("/remove")
    public ResponseEntity<String> removeProjectMember(@RequestParam Long projectId, @RequestParam Long memberId) {
        boolean isRemoved = projectMemberService.removeMemberFromProject(projectId, memberId);
        return isRemoved ? ResponseEntity.ok("Member removed successfully!") : ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Failed to remove member.");
    }
}
